import {useCallback, useEffect, useRef, useState} from "react";

export default function UseScrollToBottom() {

    const scrollRef = useRef<HTMLDivElement>(null);

    const [autoScroll, setAutoScroll] = useState<boolean>(true);

    const scrollToBottom = useCallback(() => {
        const dom = scrollRef.current;
        if (dom) {
            requestAnimationFrame(() => dom.scrollTo(0, dom.scrollHeight));
        }
    }, []);

    useEffect(() => {
        autoScroll && scrollToBottom();
    })

    return {
        scrollRef,
        autoScroll,
        setAutoScroll,
        scrollToBottom
    }
}
